import com.sun.xml.internal.ws.addressing.WsaActionUtil;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: XMQ
 * Date: 2022-05-07
 * Time: 8:05
 */
//20220506数组补充
public class TestDemo {

    public static void main(String[] args) {
        int[][] array = {{1,2,3},{4,5,6}};
        int[][] array2 = new int [2][3];
        int[][] array3 = new int[][]{{1,2,3},{4,5,6}};
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j]+" ");
            }
            System.out.println();
        }
    }
    /*
    昨日残留作业
    给定一个有序整型数组, 实现二分查找
     */
    /*
    实现一个方法 toString, 把一个整型数组转换成字符串. 例如数组 {1, 2, 3} , 返回的字符串为 "[1, 2, 3]", 注意 逗号 的位置和数量.
     */
    public static String my_toString(int[] array){
        if(array == null)
            return null;
        String ret = "[";
        for (int i = 0; i <array.length ; i++) {
            ret += array[i];
            if(i != array.length-1){
                ret += ", ";
            }
        }
        ret += "]";
        return ret;
    }
    public static void main2(String[] args) {
        int[] array = {1,2,3,4,5};
        String s = my_toString(array);
        System.out.println(s);
    }
    public static int binarySerach(int[] array,int x){
        int left = 0;
        int right = array.length-1;
        int key = x;
        int mid = (left+right)/2;
        while(left <= right){
            if(array[mid] == key){
                return mid;
            }
            if(array[mid] > key){
                right = mid - 1;
                mid = (left+right)/2;
            }
            if(array[mid] < key){
                left = mid + 1;
                mid = (left+right)/2;
            }
        }
        return -1;
    }
    public static void main1(String[] args) {
        int[] array = {1,2,3,4,5};
        int ret = binarySerach(array,3);
        System.out.println(ret);
    }
}
